package com.ruoyi.project.equipment.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysPostService;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.equipment.domain.Equipment;
import com.ruoyi.project.equipment.service.EquipmentService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.equipment.service.ProductService;
/**
 * 设备信息Controller
 *
 * @author ruoyi
 * @date 2020-07-03
 */
@RestController
@RequestMapping("/equipment/equipment")
public class EquipmentController extends BaseController
{
    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ISysUserService userService;
    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Equipment equipment)
    {
        startPage();
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        return getDataTable(list);
    }



    /**
     * 导出设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Equipment equipment)
    {
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        return util.exportExcel(list, "equipment");
    }

    /**
     * 获取设备信息详细信息
     */

    @PreAuthorize("@ss.hasPermi('equipment:equipment:query')")
    @GetMapping(value = {"/","/{equipmentId}"})
    public AjaxResult getInfo(@PathVariable(value = "equipmentId", required = false) Long equipmentId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("product", productService.selectProductAll());
        SysUser user = new SysUser();
        ajax.put("user", userService.selectUserList(user));
        if(StringUtils.isNotNull(equipmentId)){
            ajax.put(AjaxResult.DATA_TAG, equipmentService.selectEquipmentById(equipmentId));
            ajax.put("userIds", userService.selectUserListByEquipmentId(equipmentId));
        }
        return ajax;


    }

    /**
     * 返回设备查询信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:query')")
    @GetMapping("/listEquipment")
    public AjaxResult query(Equipment equipment)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("ecg", equipmentService.selectEquipmentList(equipment));
        return ajax;
    }

    /**
     * 新增设备信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Equipment equipment)
    {

        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除设备信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(equipmentService.deleteEquipmentByIds(equipmentIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.updateEquipmentStatus(equipment));
    }
}